---
title: "madagascarmaps"
output: pdf_document
date: "2023-02-16"
---

```{r setup, include=FALSE}
knitr::opts_chunk$set(echo = TRUE)
```

```{r, warning=FALSE,message=FALSE}
library(readxl)
library(tidyverse)
library(dplyr)
library(scales)
library(ggplot2)
library(lubridate)
library(rddtools)
library(magrittr)
library(broom)
library(stargazer)
library(scales)
library(broom)

Final_DC_Weekly_Survey <- read.csv("~/Desktop/DCWeekly/Final_DC_Weekly_Survey.csv", na.strings=c("","NA"))

DC_Survey <- Final_DC_Weekly_Survey %>%
  mutate(Thesis = coalesce(July_1_Thesis,
                           July_2_Thesis,
                           July_3_Thesis,
                           July_4_Thesis,
                           July_5_Thesis,
                           July_6_Thesis,
                           July_7_Thesis,
                           July_8_Thesis,
                           July_9_Thesis,
                           July_10_Thesis,
                           Oct_1_Thesis,
                           Oct_2_Thesis,
                           Oct_3_Thesis,
                           Oct_4_Thesis,
                           Oct_5_Thesis,
                           Oct_6_Thesis,
                           Oct_7_Thesis,
                           Oct_8_Thesis,
                           Oct_9_Thesis,
                           Oct_10_Thesis))

DC_Survey <- DC_Survey %>%
  mutate(Norm = coalesce(July_1_Norm,
                           July_2_Norm,
                           July_3_Norm,
                           July_4_Norm,
                           July_5_Norm,
                           July_6_Norm,
                           July_7_Norm,
                           July_8_Norm,
                           July_9_Norm,
                           July_10_Norm,
                           Oct_1_Norm,
                           Oct_2_Norm,
                           Oct_3_Norm,
                           Oct_4_Norm,
                           Oct_5_Norm,
                           Oct_6_Norm,
                           Oct_7_Norm,
                           Oct_8_Norm,
                           Oct_9_Norm,
                           Oct_10_Norm))
```

```{r}
DC_Survey <- DC_Survey %>%
  mutate(Norm_Numeric = case_when(
    Norm == "Strongly Disagree" ~ 1,
    Norm == "Somewhat Disagree" ~ 2,
    Norm == "Neither Agree nor Disagree" ~ 3,
    Norm == "Somewhat Agree" ~ 4,
    Norm == "Strongly Agree" ~ 5))

DC_Survey <- DC_Survey %>%
  mutate(Thesis_Numeric = case_when(
    Thesis == "Strongly Disagree" ~ 1,
    Thesis == "Somewhat Disagree" ~ 2,
    Thesis == "Neither Agree nor Disagree" ~ 3,
    Thesis == "Somewhat Agree" ~ 4,
    Thesis == "Strongly Agree" ~ 5))

DC_Survey <- DC_Survey %>%
  mutate(Period = case_when(
    grepl("Oct", FL_33_DO, ignore.case = TRUE) ~ "October",
    grepl("July", FL_33_DO, ignore.case = TRUE) ~ "July",
    TRUE ~ NA_character_
  ))

DC_Survey <- DC_Survey %>%
  mutate(Period_Norms = case_when(
    grepl("Oct", Norming_DO, ignore.case = TRUE) ~ "October",
    grepl("July", Norming_DO, ignore.case = TRUE) ~ "July",
    TRUE ~ NA_character_
  ))

Approved_DC <- subset(DC_Survey, Attention_Check == "Second option" & Duration..in.seconds. > 89)
```

```{r}
Approved_DC <- Approved_DC %>%
  mutate(Credibility_Numeric = case_when(
    Domain_Credibility == "Not at all credible" ~ 1,
    Domain_Credibility == "Slightly credible" ~ 2,
    Domain_Credibility == "Moderately credible" ~ 3,
    Domain_Credibility == "Credible" ~ 4,
    Domain_Credibility == "Very Credible" ~ 5))

Approved_DC <- Approved_DC %>%
  mutate(Sharing = case_when(
    Q30 == "No" ~ 1,
    Q30 == "Maybe" ~ 2,
    Q30 == "Yes" ~ 3))

Approved_DC <- Approved_DC %>%
  mutate(AI_Generated = case_when(
    Q31 == "Extremely Unlikely" ~ 1,
    Q31 == "Somewhat Unlikely" ~ 2,
    Q31 == "Neither Likely nor Unlikely" ~ 3,
    Q31 == "Somewhat Likely" ~ 4,
    Q31 == "Extremely Likely" ~ 5))
```

```{r}
extra_long_dcweekly <- Approved_DC %>%
  select(Thesis_Numeric, Credibility_Numeric, Sharing, AI_Generated, Period)

extra_long_dcweekly$Period <- factor(extra_long_dcweekly$Period, levels = c("October", "July"))

scaled_data <- extra_long_dcweekly %>%
  mutate(across(where(is.numeric), ~rescale(.x, to = c(0, 1))))

Persuasion_Credibility <- scaled_data %>% 
  pivot_longer(cols = -Period, names_to = "Variable", values_to = "Value") %>%
  group_by(Variable) %>%
  summarise(ttest = list(tidy(t.test(Value ~ Period)))) %>%
  unnest(ttest)

rename_list <- c("Credibility_Numeric" = "Domain\nCredibility",
                 "Thesis_Numeric" = "Thesis\nPersuasiveness",
                 "Sharing" = "Willingness\nto Share",
                 "AI_Generated" = "Likelihood\nof AI")

Persuasion_Credibility$Variable <- rename_list[Persuasion_Credibility$Variable]

ggplot(Persuasion_Credibility, aes(x = reorder(Variable, estimate), y = estimate, color = p.value < 0.05)) +
  geom_point(size = 3) +
  geom_errorbar(aes(ymin = conf.low, ymax = conf.high), width = 0.2, size = 1.3) +
  geom_hline(yintercept = 0, color = 'red', lty = 'dashed') +
  labs(x = '', y = '') +
  scale_y_continuous(limits = c(-.1,.1), breaks = c(-0.1,-0.05,0,0.05,0.1)) +
  scale_color_manual(values = c("grey65", "black"), 
                     labels = c("Not Significant", "Significant"),
                     name = "") +
  coord_flip() + theme_light() +
  theme(axis.text.y = element_text(face = "italic",size = 11, color = "black"),
          axis.text.x = element_text(size = 12, color = "black"),
        axis.title.x = element_text(size = 12, color = "black", face = "italic"),
        legend.position = "none")
```

```{r}
Approved_DC_Comparison <- Approved_DC %>%
  mutate(across(where(is.numeric), ~rescale(.x, to = c(0, 1))))

results1 <- Approved_DC_Comparison %>%
  group_by(Period_Norms) %>%
  summarise(
    Mean = mean(Norm_Numeric, na.rm = TRUE),
    Lower = Mean - qt(0.975, df=n()-1) * sd(Norm_Numeric, na.rm = TRUE) / sqrt(n()),
    Upper = Mean + qt(0.975, df=n()-1) * sd(Norm_Numeric, na.rm = TRUE) / sqrt(n())
  )

results2 <- Approved_DC_Comparison %>%
  group_by(Period) %>%
  summarise(
    Mean = mean(Thesis_Numeric, na.rm = TRUE),
    Lower = Mean - qt(0.975, df=n()-1) * sd(Thesis_Numeric, na.rm = TRUE) / sqrt(n()),
    Upper = Mean + qt(0.975, df=n()-1) * sd(Thesis_Numeric, na.rm = TRUE) / sqrt(n())
  )

comparison <- left_join(results1, results2, by = c("Period_Norms" = "Period"))
```

```{r}
plot_data <- comparison %>%
  pivot_longer(
    cols = -Period_Norms, 
    names_to = c(".value", "Type"), 
    names_pattern = "(.*)\\.(.*)"
  )

plot_data$Period_Norms <- factor(plot_data$Period_Norms, levels = c("October", "July"))

ggplot(plot_data, aes(x = Period_Norms, y = Mean, ymin = Lower, ymax = Upper, color = Type)) +
  geom_pointrange(lwd=1, size = 0.4) +
  labs(title = "", x = "", y = "") +
  theme_minimal() +
  scale_color_manual(values = c("grey", "black"), labels = c("Baseline", "Propaganda")) +
  geom_text(aes(label = sprintf("%.3f", Mean), vjust = -0.5)) +
  coord_flip() + theme_light() +
  theme(axis.text.y = element_text(face = "italic",size = 11, color = "black"),
          axis.text.x = element_text(size = 12, color = "black"),
        axis.title.x = element_text(size = 12, color = "black", face = "italic"),
        legend.position = "bottom",
        legend.title = element_blank(),
        plot.title = element_text(hjust = 0.5))
```


```{r}
scaled_data$Period <- factor(scaled_data$Period, levels = c("July", "October"))

thesis_t_test <- t.test(Thesis_Numeric ~ Period, data = scaled_data, na.action = na.exclude)
cred_t_test <- t.test(Credibility_Numeric ~ Period, data = scaled_data, na.action = na.exclude)
ai_t_test <- t.test(AI_Generated ~ Period, data = scaled_data, na.action = na.exclude)
sharing_t_test <- t.test(Sharing ~ Period, data = scaled_data, na.action = na.exclude)

results <- data.frame(
  Variable = c("Thesis_Numeric", "Credibility_Numeric", "AI_Generated", "Sharing"),
  Estimate = c(thesis_t_test$estimate[[2]] - thesis_t_test$estimate[[1]],
               cred_t_test$estimate[[2]] - cred_t_test$estimate[[1]],
               ai_t_test$estimate[[2]] - ai_t_test$estimate[[1]],
              sharing_t_test$estimate[[2]] - sharing_t_test$estimate[[1]]),
  Std.Error = c(thesis_t_test$stderr, cred_t_test$stderr, ai_t_test$stderr, sharing_t_test$stderr),
  t.value = c(thesis_t_test$statistic, cred_t_test$statistic, ai_t_test$statistic, sharing_t_test$statistic),
  Pr = c(thesis_t_test$p.value, cred_t_test$p.value, ai_t_test$p.value, sharing_t_test$p.value)
)

stargazer(results, summary = FALSE, type = "text",
          title = "Results of T-tests",
          align = TRUE,
          header = FALSE,
          covariate.labels = c("Difference in Means", "Standard Error", "t-value", "P-value"),
          dep.var.labels.include = FALSE,
          rownames = FALSE,
          initial.zero = FALSE, 
          digits = 3,  
          star.cutoffs = c(0.05, 0.01, 0.001))
```



Ukraine specific support:
```{r}
Approved_DC <- Approved_DC %>%
  mutate(Support_Aid = case_when(
    Foreign_Aid == "Too much support to Ukraine" ~ 1,
    Foreign_Aid == "About the right amount of support to Ukraine" ~ 2,
    Foreign_Aid == "Not enough support to Ukraine" ~ 3,
    Foreign_Aid == "Unsure" ~ NA_real_))

Approved_DC <- Approved_DC %>%
  mutate(FightBack = case_when(
    Policy_Preference == "Try to end the conflict as quickly as possible, even if it means allowing Russia to keep territory it has captured from Ukraine" ~ 0,
    Policy_Preference == "Support Ukraine in reclaiming territory Russia has captured, even if it results in a more prolonged conflict between the two nations" ~ 1,
    Foreign_Aid == "Unsure" ~ NA_real_))

Approved_DC <- Approved_DC %>%
  mutate(RussiaResponsible = case_when(
    Responsibility == "Russia" ~ 0,
    Responsibility == "USA/NATO/EU" ~ 1,
    Responsibility == "Ukraine" ~ 1,
    Responsibility == "Other" ~ 1))

Approved_DC_Scaled <- Approved_DC %>%
  mutate(across(where(is.numeric), ~rescale(.x, to = c(0, 1))))
```

```{r}
support_aid <- t.test(Support_Aid ~ Period, data = Approved_DC_Scaled, na.action = na.exclude)
continue_fighting <- t.test(FightBack ~ Period, data = Approved_DC_Scaled, na.action = na.exclude)
russia_responsible <- t.test(RussiaResponsible ~ Period, data = Approved_DC_Scaled, na.action = na.exclude)

results_ukraine <- data.frame(
  Variable = c("support_aid", "continue_fighting", "russia_responsible"),
  Estimate = c(support_aid$estimate[[2]] - support_aid$estimate[[1]],
               continue_fighting$estimate[[2]] - continue_fighting$estimate[[1]],
               russia_responsible$estimate[[2]] - russia_responsible$estimate[[1]]),
  Std.Error = c(support_aid$stderr, continue_fighting$stderr, russia_responsible$stderr),
  t.value = c(support_aid$statistic, continue_fighting$statistic, russia_responsible$statistic),
  Pr = c(support_aid$p.value, continue_fighting$p.value, russia_responsible$p.value)
)

stargazer(results_ukraine, summary = FALSE, type = "text",
          title = "Results of T-tests",
          align = TRUE,
          header = FALSE,
          covariate.labels = c("Difference in Means", "Standard Error", "t-value", "P-value"),
          dep.var.labels.include = FALSE,
          rownames = FALSE,
          initial.zero = FALSE,  
          digits = 3,  
          star.cutoffs = c(0.05, 0.01, 0.001))  
```


```{r}
Summary_Stats <- Approved_DC %>%
  select(Thesis_Numeric, Credibility_Numeric, AI_Generated, Sharing, Support_Aid, FightBack, RussiaResponsible)

Summary_Stats <- Summary_Stats %>% 
  pivot_longer(cols = everything(), names_to = "Variables", values_to = "values")

summary_table <- Summary_Stats %>%
  group_by(Variables) %>%
  summarize(
    Mean = mean(values, na.rm = TRUE),
    SD = sd(values, na.rm = TRUE),
    Min = min(values, na.rm = TRUE),
    Max = max(values, na.rm = TRUE),
    .groups = "drop"
  )

stargazer_ready <- summary_table %>%
  pivot_longer(-Variables, names_to = "statistic", values_to = "value") %>%
  pivot_wider(names_from = statistic, values_from = value)

stargazer_ready$Mean <- round(stargazer_ready$Mean, 3)
stargazer_ready$SD <- round(stargazer_ready$SD, 3)

new_names <- c("Likelihood of AI", "Domain Credibility", "Ukraine Should Fight",
               "Russia is Responsible", "Willingness to Share", "Support More Foreign Aid", "Thesis Persuasiveness")

stargazer_ready$Variables <- new_names

stargazer(stargazer_ready, type = "text", summary = FALSE, title = "Summary Statistics")
```













